package com.niuxiaofa.newlaw.Tools;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.style.ReplacementSpan;

import java.util.regex.Pattern;

/**
 * @ClassName RoundBackgroundColorSpan
 * @Description TODO
 * @Author Gxjkz
 * @Date 2020/9/14 16:34
 * @Version 1.0
 */
public class RoundBackgroundColorSpan2 extends ReplacementSpan {
    private int bgColor;
    private int textColor;
    private int dpx=UIUtils.dp2px(UIUtils.getActivity(),2);
    private  int textdp= UIUtils.dp2px(UIUtils.getActivity(),10);
    public RoundBackgroundColorSpan2(int bgColor, int textColor) {
        super();
        this.bgColor = bgColor;
        this.textColor = textColor;
    }
    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {

        return ((int)(textdp*end)+dpx*4);
    }

    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {

        Pattern p= Pattern.compile("[a-zA-Z]");
        Pattern p2= Pattern.compile("[\\u4e00-\\u9fa5]");



        int color1 = paint.getColor();
        paint.setColor(this.bgColor);
        paint.setTextSize(textdp);
        canvas.drawRoundRect(new RectF(x, top+dpx, x + ((int)(textdp*end)+dpx*2), bottom-dpx), 7, 7, paint);
        paint.setColor(this.textColor);


        canvas.drawText(text, start, end, x + dpx, y - dpx, paint);
        paint.setColor(color1);
    }
}
